package com.ruoyi.flowable.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.flowable.domain.bo.DraftBo;
import com.ruoyi.flowable.domain.vo.DraftVo;
import com.ruoyi.flowable.domain.Draft;
import com.ruoyi.flowable.mapper.DraftMapper;
import com.ruoyi.flowable.service.IDraftService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 表单草稿Service业务层处理
 *
 * @author zxw
 * @date 2023-05-16
 */
@RequiredArgsConstructor
@Service
public class DraftServiceImpl implements IDraftService {

    private final DraftMapper baseMapper;

    /**
     * 查询表单草稿
     */
    @Override
    public DraftVo queryById(Long draftId){
        return baseMapper.selectVoById(draftId);
    }

    /**
     * 查询表单草稿列表
     */
    @Override
    public TableDataInfo<DraftVo> queryPageList(DraftBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<Draft> lqw = buildQueryWrapper(bo);
        lqw.orderByDesc(Draft::getDraftId);
        Page<DraftVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询表单草稿列表
     */
    @Override
    public List<DraftVo> queryList(DraftBo bo) {
        LambdaQueryWrapper<Draft> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<Draft> buildQueryWrapper(DraftBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<Draft> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getUserName()), Draft::getUserName, bo.getUserName());
        lqw.eq(bo.getAppId() != null, Draft::getAppId, bo.getAppId());
        return lqw;
    }

    /**
     * 新增表单草稿
     */
    @Override
    public Boolean insertByBo(DraftBo bo) {
        Draft add = BeanUtil.toBean(bo, Draft.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setDraftId(add.getDraftId());
        }
        return flag;
    }

    /**
     * 修改表单草稿
     */
    @Override
    public Boolean updateByBo(DraftBo bo) {
        Draft update = BeanUtil.toBean(bo, Draft.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(Draft entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除表单草稿
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
